package org.takeback.chat.lottery.listeners;

import java.math.BigDecimal;

import javax.annotation.Resource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.takeback.chat.entity.GcLottery;
import org.takeback.chat.entity.Message;
import org.takeback.chat.lottery.Lottery;
import org.takeback.chat.lottery.LotteryDetail;
import org.takeback.chat.service.Game01Service;
import org.takeback.chat.service.GameG07Service;
import org.takeback.chat.store.room.LotteryFactory;
import org.takeback.chat.store.room.Room;
import org.takeback.chat.store.user.User;
import org.takeback.chat.utils.MessageUtils;
import org.takeback.mvc.ResponseUtils;
import org.takeback.util.BeanUtils;

@Component("G07")
public class G07 extends DefaultGameListener {
	
	@Resource(name="gameG07Service")
	private GameG07Service gameG07Service;

	public void onFinished(Lottery lottery) throws GameException {
		super.onFinished(lottery);
		if ("2".equals(lottery.getType())) {
			Room room = (Room) this.roomStore.get(lottery.getRoomId());
			int i = this.lotteryService.setLotteryFinished(lottery.getId());
			String sendNickName = null;
			if (0 == lottery.getSender().intValue()) {
				sendNickName = "系统";
			} else {
				User sender = (User) this.userStore.get(lottery.getSender());
				sendNickName = sender.getNickName();
			}
			this.gameG07Service.dealResult(lottery, room);
		}
	}

	public boolean onBeforeRed(LotteryFactory.DefaultLotteryBuilder builder) throws GameException {
		Room room = builder.getRoom();
		int master = room.getMaster().intValue();
		int sender = builder.getSender();
		User user = (User) this.userStore.get(Integer.valueOf(sender));
		if(!"admin".equals(user.getUserId())){
			throw new GameException(500, "只有房间管理员才能发包");
		}
		int maxSize = Integer.valueOf(getConifg(room.getId(), "config_lhl_hb_size")).intValue();
		if (builder.getNumber() != maxSize) {
			throw new GameException(500, "房间限制红包个数必须为:"  + maxSize + "个");
		}
		Double maxMoney = Double.valueOf(getConifg(room.getId(), "conf_lhh_money"));
		if (builder.getMoney().doubleValue() != maxMoney.doubleValue()) {
			throw new GameException(500, "红包金额限制:" +  maxMoney);
		}
		builder.setType("2");

		int expired = Integer.valueOf(getConifg(room.getId(), "conf_expired")).intValue();

//		String raid = builder.getDescription();
//		Integer raidPoint;
//		try {
//			raidPoint = Integer.valueOf(raid);
//		} catch (Exception e) {
//			throw new GameException(500, "雷点数为0-9的个位数字");
//		}
//		if ((raidPoint.intValue() > 9) || (raidPoint.intValue() < 0)) {
//			throw new GameException(500, "雷点数为0-9的个位数字");
//		}
//		double water = getWater(room, Double.valueOf(builder.getMoney().doubleValue())).doubleValue();
		double cutMoney = builder.getMoney().doubleValue();
//		builder.setMoney(NumberUtil.round(builder.getMoney().subtract(new BigDecimal(water))));
//		builder.setDescription(builder.getMoney() + "金币/雷" + raidPoint);
		builder.setExpiredSeconds(Integer.valueOf(expired));
		int affected = this.lotteryService.moneyDown(Integer.valueOf(sender), Double.valueOf(cutMoney));
		if (affected == 0) {
			throw new GameException(500, "金额不足!余额必须大于" + cutMoney);
		}
		return true;
	}

	public void processExpireEvent(Lottery lottery) throws GameException {
		super.processExpireEvent(lottery);

//		Room room = (Room) this.roomStore.get(lottery.getRoomId());
//		room.setStatus("0");
//
//		this.game01Service.gameLotteryExpired(lottery, room);
//		Message msg = new Message("TXT_SYS", Integer.valueOf(0), "<span style='color:#B22222'>游戏包过期,游戏停止</span>");
//		MessageUtils.broadcast(room, msg);
	}

	public boolean onBeforeOpen(Integer uid, Lottery lottery) throws GameException {
		if ("1".equals(lottery.getType())) {
			return true;
		}
		Double money = Double.valueOf(getConifg(lottery.getRoomId(), "conf_get_less_money"));
		if (this.lotteryService.moneyDown(uid, money) < 1) {
			throw new GameException(500, "金额不能少于" + money + "元,请及时充值!");
		}
		return true;
	}

	public void processStartEvent(Room room) throws GameException {
		super.processStartEvent(room);
//		333 333
//		qqqq qqq
//		eeee eeee
//		Integer number = Integer.valueOf(getConifg(room.getId(), "conf_size"));
//		Double money = Double.valueOf(getConifg(room.getId(), "conf_money_start"));
//		Integer expreid = Integer.valueOf(getConifg(room.getId(), "conf_expired"));
//
//		Lottery lottery = LotteryFactory.getDefaultBuilder(BigDecimal.valueOf(money.doubleValue()), number)
//				.setDescription("游戏开始,祝你好运!").setSender(0).setType("2").setExpiredSeconds(expreid).setRoom(room)
//				.build();
//		GcLottery gcLottery = (GcLottery) BeanUtils.map(lottery, GcLottery.class);
//		this.lotteryService.save(GcLottery.class, gcLottery);
//		this.lotteryService.setRoomStatus(room.getId(), "1");
//		room.setStatus("1");
//		Message message = new Message("RED", Integer.valueOf(0), lottery);
//		message.setHeadImg(User.SYSTEM_HEADIMG);
//		message.setNickName("系统");
//		MessageUtils.broadcast(room, message);
	}

	public void onOpen(Lottery lottery, LotteryDetail lotteryDetail) throws GameException {
		super.onOpen(lottery, lotteryDetail);
		if ("2".equals(lottery.getType())) {
			String sendNickName = null;
			User opener = (User) this.userStore.get(lotteryDetail.getUid());
			if (0 == lottery.getSender().intValue()) {
				sendNickName = "系统";
			} else {
				User sender = (User) this.userStore.get(lottery.getSender());
				if (opener.getId().equals(sender.getId())) {
					sendNickName = "自己";
				} else {
					sendNickName = sender.getNickName();
				}
			}
			String msg = "<span style='color:#F89C4C'>" + opener.getNickName()
					+ "</span> 领取了<span style='color:#F89C4C'>" + sendNickName + "</span>发的红包";
			Message notice = new Message("TXT_SYS", Integer.valueOf(0), msg);
			MessageUtils.broadcast((Room) this.roomStore.get(lottery.getRoomId()), notice);
		}
	}

	public boolean onBeforeStart(Room room) throws GameException {
		return true;
	}
}
